//
//  BoxCell.m
//  OrderSeat
//
//  Created by haiming on 13-10-11.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "BoxCell.h"
#import "UIImageView+WebCache.h"

#define ORDER_IMAGE [UIImage imageNamed:@"bookingbox_btn4_n.png"]
#define SELECTED_IMAGE [UIImage imageNamed:@"bookingbox_btn3_n.png"]
#define NORMAL_IMAGE [UIImage imageNamed:@"bookingbox_btn2_n.png"]

@implementation BoxCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setContent:(NSMutableDictionary *)dic andIndex:(NSInteger)index
{
    self.index = index;
    self.dictionary = dic;
    
    [_logoIV setImageWithURL:[dic objectForKey:@"image"]];
    _seatNameLabel.text = [dic objectForKey:@"zwname"];
    _peopleNumLabel.text = [[dic objectForKey:@"renshu"] stringValue];
    _areaLabel.text = [dic objectForKey:@"size"];
    _installationLabel.text = [dic objectForKey:@"sheshi"];
    
    if([[_dictionary objectForKey:@"zhuangtai"] intValue] == 2) {
        [_orderBtn setBackgroundImage:ORDER_IMAGE forState:UIControlStateNormal];
    }
    else if([[_dictionary objectForKey:@"selected"] intValue] == 1) {
        [_orderBtn setBackgroundImage:SELECTED_IMAGE forState:UIControlStateNormal];
    }
    else if([[_dictionary objectForKey:@"selected"] intValue] == 0){
        [_orderBtn setBackgroundImage:NORMAL_IMAGE forState:UIControlStateNormal];
    }
    
    [_logoIV addTarget:self action:@selector(imagePhotoAction:)];
    [self reLayoutSubviews];
}

- (void)reLayoutSubviews
{
    [_view drawBorder];
    
    [_peopleNumLabel autoWidthForSingleLine];
    [_peopleNumLeftLabel autoWidthForSingleLine];
    [_peopleNumRightLabel autoWidthForSingleLine];
    
    _peopleNumLabel.left = _peopleNumLeftLabel.right_1;
    _peopleNumRightLabel.left = _peopleNumLabel.right_1;
    
    [_areaLabel autoWidthForSingleLine];
    [_areaLeftLabel autoWidthForSingleLine];
    [_areaRightLabel autoWidthForSingleLine];
    
    _areaLeftLabel.left = _peopleNumRightLabel.right_10;
    _areaLabel.left = _areaLeftLabel.right_1;
    _areaRightLabel.left = _areaLabel.right_1;
    
    _installationLabel.top = _peopleNumLabel.bottom_3;
    [_installationLabel autoWidthForSingleLine];
}

- (void)imagePhotoAction:(id)sender
{
    [_delegate tapImageWithIndex:self.index];
}

- (IBAction)orderAction:(id)sender
{
    if([[_dictionary objectForKey:@"zhuangtai"] intValue] == 2) return;
    
    NSString *selected = [_dictionary objectForKey:@"selected"];
    selected = [selected isEqualToString:@"0"] ? @"1" : @"0";
    
    [_dictionary setObject:selected forKey:@"selected"];
    
    if ([selected intValue] == 1) {
        [_orderBtn setBackgroundImage:SELECTED_IMAGE forState:UIControlStateNormal];
    }else {
        [_orderBtn setBackgroundImage:NORMAL_IMAGE forState:UIControlStateNormal];
    }
    
    if (_delegate) {
        [_delegate selectedFinished];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end
